﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using IPC.Communication.Core;
using IPC.Communication.Core.IMessage;
using IPC.Communication.Core.Net;
using IPC.Communication.Reflection;

namespace IPC.Communication.Profinet.IDCard
{
    public class SAMTcpNet : NetworkDoubleBase
    {
        public SAMTcpNet()
        {
            base.ByteTransform = new RegularByteTransform();
        }

        public SAMTcpNet(string ipAddress, int port)
        {
            IpAddress = ipAddress;
            Port = port;
            base.ByteTransform = new RegularByteTransform();
        }

        protected override INetMessage GetNewNetMessage()
        {
            return new SAMMessage();
        }

        [HslMqttApi]
        public OperateResult<string> ReadSafeModuleNumber()
        {
            byte[] send = SAMSerial.PackToSAMCommand(SAMSerial.BuildReadCommand(18, byte.MaxValue, null));
            OperateResult<byte[]> operateResult = ReadFromCoreServer(send);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<string>(operateResult);
            }
            OperateResult operateResult2 = SAMSerial.CheckADSCommandAndSum(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<string>(operateResult2);
            }
            return SAMSerial.ExtractSafeModuleNumber(operateResult.Content);
        }

        [HslMqttApi]
        public OperateResult CheckSafeModuleStatus()
        {
            byte[] send = SAMSerial.PackToSAMCommand(SAMSerial.BuildReadCommand(18, byte.MaxValue, null));
            OperateResult<byte[]> operateResult = ReadFromCoreServer(send);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<string>(operateResult);
            }
            OperateResult operateResult2 = SAMSerial.CheckADSCommandAndSum(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<string>(operateResult2);
            }
            if (operateResult.Content[9] != 144)
            {
                return new OperateResult(SAMSerial.GetErrorDescription(operateResult.Content[9]));
            }
            return OperateResult.CreateSuccessResult();
        }

        [HslMqttApi]
        public OperateResult SearchCard()
        {
            byte[] send = SAMSerial.PackToSAMCommand(SAMSerial.BuildReadCommand(32, 1, null));
            OperateResult<byte[]> operateResult = ReadFromCoreServer(send);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<string>(operateResult);
            }
            OperateResult operateResult2 = SAMSerial.CheckADSCommandAndSum(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<string>(operateResult2);
            }
            if (operateResult.Content[9] != 159)
            {
                return new OperateResult(SAMSerial.GetErrorDescription(operateResult.Content[9]));
            }
            return OperateResult.CreateSuccessResult();
        }

        [HslMqttApi]
        public OperateResult SelectCard()
        {
            byte[] send = SAMSerial.PackToSAMCommand(SAMSerial.BuildReadCommand(32, 2, null));
            OperateResult<byte[]> operateResult = ReadFromCoreServer(send);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<string>(operateResult);
            }
            OperateResult operateResult2 = SAMSerial.CheckADSCommandAndSum(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<string>(operateResult2);
            }
            if (operateResult.Content[9] != 144)
            {
                return new OperateResult(SAMSerial.GetErrorDescription(operateResult.Content[9]));
            }
            return OperateResult.CreateSuccessResult();
        }

        [HslMqttApi]
        public OperateResult<IdentityCard> ReadCard()
        {
            byte[] send = SAMSerial.PackToSAMCommand(SAMSerial.BuildReadCommand(48, 1, null));
            OperateResult<byte[]> operateResult = ReadFromCoreServer(send);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<IdentityCard>(operateResult);
            }
            OperateResult operateResult2 = SAMSerial.CheckADSCommandAndSum(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<IdentityCard>(operateResult2);
            }
            return SAMSerial.ExtractIdentityCard(operateResult.Content);
        }

        //[AsyncStateMachine(typeof(<ReadSafeModuleNumberAsync>d__8))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<string>> ReadSafeModuleNumberAsync()
        //{
        //    <ReadSafeModuleNumberAsync>d__8 stateMachine = new <ReadSafeModuleNumberAsync>d__8();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<CheckSafeModuleStatusAsync>d__9))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> CheckSafeModuleStatusAsync()
        //{
        //    <CheckSafeModuleStatusAsync>d__9 stateMachine = new <CheckSafeModuleStatusAsync>d__9();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<SearchCardAsync>d__10))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> SearchCardAsync()
        //{
        //    <SearchCardAsync>d__10 stateMachine = new <SearchCardAsync>d__10();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<SelectCardAsync>d__11))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> SelectCardAsync()
        //{
        //    <SelectCardAsync>d__11 stateMachine = new <SelectCardAsync>d__11();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadCardAsync>d__12))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<IdentityCard>> ReadCardAsync()
        //{
        //    <ReadCardAsync>d__12 stateMachine = new <ReadCardAsync>d__12();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<IdentityCard>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        public override string ToString()
        {
            return String.Format("SAMTcpNet[{0}:{1}]", IpAddress, Port);
        }
    }
}